package com.bigShen.pattern.singleton.regist;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @auther: lei.shen
 * @Date: 2018/10/1 16:51
 * @Description:
 */
public class BeanFactory {

    private BeanFactory() {
    }

    private static final Map<String, Object> factory = new ConcurrentHashMap<String, Object>();

    public static synchronized Object getBean (String className) {

        if (factory.containsKey(className)) {
            return factory.get(className);
        } else {
            Object bean = null;
            try {
                bean = Class.forName(className).newInstance();
                factory.put(className, bean);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
            return bean;
        }

    }

}
